using System;
using TrainGame.Model;

namespace _1830.UI
{
	/// <summary>
	/// Summary description for WebForm2.
	/// </summary>
	public partial class StartGameForm : System.Web.UI.Page 
	{
	
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ID = "StartGameForm";

		}
		#endregion

		protected void Page_Load(object sender, System.EventArgs e)
		{
			string sNumberOfPlayers;
			String sPlayer;
            Game game = Global.getGame (this);

			if (game.Started)
			{
				game.Started = true;

                string sURL = "PrivateSales.aspx?PlayerName=" + Session["PlayerName"].ToString ();
				Response.Redirect(sURL);
			}

			labelGameName.Text = game.GameName;
			sNumberOfPlayers = game.Players.Count.ToString ();
			numberOfPlayers.Text = sNumberOfPlayers;
			playersListbox.Items.Clear ();

			foreach (Player player in game.Players)
				playersListbox.Items.Add (player.Name);

			sPlayer = Session["PlayerName"].ToString ();

			if (game.Creator == sPlayer)
			{
				StartButton.Enabled = true;
                AllowUseOfOptional6Train.Enabled = true;
				CollusionAllowed.Enabled = true;
			}
			else
			{
				StartButton.Enabled = false;
                AllowUseOfOptional6Train.Enabled = false;
				CollusionAllowed.Enabled = false;
			}

			if (!IsPostBack)
			{
                AllowUseOfOptional6Train.Checked = game.AllowUseOfOptional6Train;
				CollusionAllowed.Checked = game.CollusionAllowed;
			}
		}

		protected void StartButton_Click(object sender, System.EventArgs e)
		{
			Game game = Global.getGame (this);
			string sPlayer = Session["PlayerName"].ToString ();

			if (game.Creator == sPlayer)
			{
                game.AllowUseOfOptional6Train = AllowUseOfOptional6Train.Checked;

				StartGame ();
			}
			else
			{
				string sError;

				sError = "Only the game's creator, " + game.Creator + " can start the game.";
				statusLabel.Text = sError;
			}
		}

        protected void AllowUseOfOptional6Train_Changed(object sender, System.EventArgs e)
		{
			Game game = Global.getGame (this);
			string sPlayer = Session["PlayerName"].ToString ();

			if (game.Creator == sPlayer)
                game.AllowUseOfOptional6Train = AllowUseOfOptional6Train.Checked;
		}

		private void StartGame ()	// Go to the initial round page.
		{
			Game game = Global.getGame (this);
			
            game.Started = true;
            game.CalculateNewCertificateLimit();    // ensure we have the correct certificate limit at game start

            string sURL = "PrivateSales.aspx?PlayerName=" + Session["PlayerName"].ToString ();
			Response.Redirect(sURL);
		}

	}
}
